package com.yyk.yykywd.mvvm.adapter

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yyk.yykywd.*
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.mvvm.entity.HomeBean
import com.yyk.yykywd.mvvm.entity.MaterialBean
import com.yyk.yykywd.mvvm.entity.ReturnMoney
import com.yyk.yykywd.util.DensityUtils
import com.yyk.yykywd.util.GlideUtils
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.widght.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_contract_img.*
import java.util.ArrayList

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class MaterialAdapter(private val context: Context,
                      layoutResId: Int,
                      data: List<MaterialBean.Data>) : BaseQuickAdapter<MaterialBean.Data, BaseViewHolder>(layoutResId, data) {

    override fun convert(helper: BaseViewHolder, item: MaterialBean.Data) {
        helper.setText(R.id.tv_name, cutTwoCharacter(item.name))
        helper.setText(R.id.tv_username, item.name)
        helper.setText(R.id.tv_department, item.depname)
        helper.setText(R.id.tv_time, getCommonTime(item.create_time))
        helper.setText(R.id.tv_content, item.content)
        helper.addOnClickListener(R.id.tv_del)
        val tvDel: TextView = helper.getView(R.id.tv_del)
        val tvContent: TextView = helper.getView(R.id.tv_content)
        val rv: RecyclerView = helper.getView(R.id.rv)
        val list: MutableList<String> = ArrayList()
        val list1: MutableList<String> = ArrayList()
        item.pics?.run {
            list.addAll(stringToList(this).map { HttpUrl.BASE_URL + it })
            list1.addAll(stringToList(this))
        }
        rv.layoutManager = GridLayoutManager(context, 3)
        if (rv.itemDecorationCount == 0) {
            rv.addItemDecoration(SpaceItemDecoration(DensityUtils.dp2px(context, 10f)))
        }
        val imageViewAdapter = ImageViewAdapter(context, R.layout.item_iv, list1)
        rv.adapter = imageViewAdapter

        imageViewAdapter.setOnItemClickListener { _, _, position ->
            ToAnotherActivity.toPicActivity(context, list[position])
        }
        if (item.is_del_show == 1) {
            tvDel.visibility = View.VISIBLE
        } else {
            tvDel.visibility = View.GONE
        }
        if (item.content.isNullOrBlank()) {
            tvContent.visibility = View.GONE
        } else {
            tvContent.visibility = View.VISIBLE
        }

        if (item.pics.isNullOrBlank()) {
            rv.visibility = View.GONE
        } else {
            rv.visibility = View.VISIBLE
        }
    }

}